﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ComTools
{
    public partial class frmSetComPort : Form
    {
        public frmSetComPort()
        {
            InitializeComponent();
        }

        private void frmSetComPort_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void Init()
        {
            FillPorts();

            string[] bitsSource = Enum.GetNames(typeof(System.IO.Ports.StopBits));
            cmbStopBits.DataSource = bitsSource;
            cmbStopBits.SelectedIndex = Array.IndexOf(bitsSource, System.IO.Ports.StopBits.One.ToString());
            string[] paritySource = Enum.GetNames(typeof(System.IO.Ports.Parity));
            cmbParity.DataSource = paritySource;
            cmbParity.SelectedIndex = Array.IndexOf(paritySource, System.IO.Ports.Parity.None.ToString());

            BaudRate = 9600;
            DataBits = 8;
        }

        private void FillPorts()
        {
            string nowPort = string.Empty;
            if (cmbSerialPort.SelectedIndex != -1)
            {
                nowPort = cmbSerialPort.SelectedItem.ToString();
            }
            cmbSerialPort.Items.Clear();
            string[] data = System.IO.Ports.SerialPort.GetPortNames();
            foreach (var s in data)
            {
                cmbSerialPort.Items.Add(s);
            }
            if (!string.IsNullOrEmpty(nowPort))
            {
                cmbSerialPort.SelectedItem = nowPort;
            }
            else if (cmbSerialPort.Items.Count > 0)
            {
                cmbSerialPort.SelectedIndex = 0;
            }
        }

        public string PortName
        {
            get
            {
                if (cmbSerialPort.SelectedIndex == -1)
                    return string.Empty;
                else
                return cmbSerialPort.SelectedItem.ToString();
            }
        }

        public int BaudRate
        {
            get
            {
                if (cmbSerialPort.SelectedIndex != -1)
                {
                    string v = cmbBaudRate.SelectedItem.ToString();
                    return int.Parse(v);
                } else
                {
                    return 9600;
                }
            }
            set
            {
                cmbBaudRate.SelectedItem = value.ToString();
            }
        }

        public int DataBits
        {
            get
            {
                string v = txtDataBits.Text.Trim();
                return Convert.ToInt32(v);
            }
            set => txtDataBits.Text = value.ToString();
        }

        public System.IO.Ports.StopBits StopBits
        {
            get
            {
                string v = cmbStopBits.SelectedItem.ToString();
                return (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), v);
            }
            set
            {
                cmbStopBits.SelectedItem = value.ToString();
            }
        }

        public System.IO.Ports.Parity Parity
        {
            get
            {
                string v = cmbParity.SelectedItem.ToString();
                return (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), v);
            }
            set
            {
                cmbParity.SelectedItem = value.ToString();
            }
        }

        private void btnFill_Click(object sender, EventArgs e)
        {
            FillPorts();
        }
    }
}